package com.zbkj.crmeb.front.controller;

import com.common.CommonResult;
import com.common.PageParamRequest;
import com.constants.Constants;
import com.exception.ApiException;
import com.utils.MyAssert;
import com.utils.SpringUtil;
import com.zbkj.crmeb.front.request.StoreNearRequest;
import com.zbkj.crmeb.front.request.UpdateDto;
import com.zbkj.crmeb.front.response.StoreNearResponse;
import com.zbkj.crmeb.front.service.UpdateService;
import com.zbkj.crmeb.system.service.SystemConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.net.InetAddress;
import java.net.UnknownHostException;

@Slf4j
@RestController("UpdateController")
@RequestMapping("api/front/update")
@Api(tags = "升级")
public class UpdateController {
    @Autowired
    UpdateService updateService;
    @Autowired
    SystemConfigService systemConfigService;


    @ApiOperation(value = "分享跳转链接")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public RedirectView index(HttpServletRequest request, @RequestParam(required = false) String spread) {
        if (!StringUtils.isEmpty(spread)) {
            String clientIp = getIpAddr(request);
            if (clientIp != null)
                updateService.createTempIpRecord(spread, clientIp);
        }
        String directUrl = systemConfigService.getValueByKey(Constants.CONFIG_APP_LOCATION);
        MyAssert.assertNotEmpty(directUrl,new ApiException("app跳转链接未配置"));
        RedirectView redirectTarget = new RedirectView();
        redirectTarget.setContextRelative(false);

        redirectTarget.setUrl(directUrl);
        return redirectTarget;
    }

    @PostMapping("check")
    @ApiOperation("查询升级")
    public CommonResult<Object> getUpdateInfo(@RequestBody @Valid UpdateDto.UpdateReq req) throws Throwable {
        return CommonResult.success(updateService.getUpdateInfo(req));
    }

    private static String getIpAddr(HttpServletRequest request) {
        String ipAddress = null;
        try {
            ipAddress = request.getHeader("x-forwarded-for");
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getRemoteAddr();
                if (ipAddress.equals("127.0.0.1")) {
                    // 根据网卡取本机配置的IP
                    InetAddress inet = null;
                    try {
                        inet = InetAddress.getLocalHost();
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    }
                    ipAddress = inet.getHostAddress();
                }
            }
            // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
            if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
                // = 15
                if (ipAddress.indexOf(",") > 0) {
                    ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
                }
            }
        } catch (Exception e) {
            ipAddress = "";
        }


        return ipAddress;
    }
}
